<template>
  <div>
    <form class="form">
      <p class="title">设置</p>
      <p class="message">设置模板文件夹</p>
      <label>
        <input v-model="filePath" required="" class="input" />
        <span>路径</span>
      </label>
      <label>
        <input  v-model.number="field" required="" class="input" type="number"/>
        <span>字段所在行</span>
      </label>
      <label>
        <input v-model.number="start"  required="" class="input" type="number"/>
        <span>数据开始行</span>
      </label>
      <label>
        <input  v-model="cloName" required="" class="input" />
        <span>SKU字段名</span>
      </label>
      <label class="account-input">
        <input v-model="account" required="" class="input" @focus="showAccountOptions = true" @blur="hideAccountOptions" />
        <span>账号</span>
        <div v-if="showAccountOptions" class="dropdown-options">
          <div class="option" v-for="option in accountOptions" :key="option" @mousedown.prevent="selectAccount(option)">{{ option }}</div>
        </div>
      </label>
      <button class="submit" type="button" @click="Onsubmit">确定</button>
    </form>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'

const filePath = ref('')
const cloName= ref('sku')
const start= ref(4)
const field= ref(3)
const account= ref("lbl")
const showAccountOptions = ref(false)
const accountOptions = ref([])
function selectAccount(value) {
  account.value = value
  showAccountOptions.value = false
}

function hideAccountOptions() {
  setTimeout(() => {
    showAccountOptions.value = false
  }, 200)
}

function Onsubmit() {
  window.localStorage.setItem('filePath', filePath.value)
  window.localStorage.setItem('cloName',cloName.value)
  window.localStorage.setItem('start',start.value)
  window.localStorage.setItem('field',field.value)
  window.localStorage.setItem('account',account.value)
  window.close()
}

onMounted(() => {
  filePath.value = window.localStorage.getItem('filePath')
  if(window.localStorage.getItem('cloName')){cloName.value = window.localStorage.getItem('cloName')}
  if(window.localStorage.getItem('start')){start.value = Number(window.localStorage.getItem('start'))}
  if(window.localStorage.getItem('field')){field.value = Number(window.localStorage.getItem('field'))}
  if(window.localStorage.getItem('account')){account.value = window.localStorage.getItem('account')}

  axios.get('https://api.xshop.fun/tables').then(response => {
    console.log(response.data.results)
    if(response.data.results.length > 0){
      response.data.results.forEach(item => {
        accountOptions.value.push(item.name)
      })
    }
  }).catch(error => {
    console.error('Error fetching accounts:', error)
  })
})
</script>

<style scoped>
.form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 350px;
  background-color: #fff;
  padding: 20px;
  border-radius: 20px;
  position: relative;
}

.title {
  font-size: 25px;
  color: royalblue;
  font-weight: 500;
  letter-spacing: -1px;
  position: relative;
  display: flex;
  align-items: center;
  padding-left: 25px;
}

.title::before,
.title::after {
  position: absolute;
  content: '';
  height: 16px;
  width: 16px;
  border-radius: 50%;
  left: 0px;
  background-color: royalblue;
}

.title::before {
  width: 18px;
  height: 18px;
  background-color: royalblue;
}

.title::after {
  width: 18px;
  height: 18px;
  animation: pulse 1s linear infinite;
}

.message,
.signin {
  color: rgba(88, 87, 87, 0.822);
  font-size: 14px;
}

.signin {
  text-align: center;
}

.signin a {
  color: royalblue;
}

.signin a:hover {
  text-decoration: underline royalblue;
}

.flex {
  display: flex;
  width: 100%;
  gap: 6px;
}

.form label {
  position: relative;
}

.form label .input {
  width: 100%;
  padding: 10px 10px 20px 10px;
  outline: 0;
  border: 1px solid rgba(105, 105, 105, 0.397);
  border-radius: 10px;
}

.form label .input + span {
  position: absolute;
  left: 10px;
  top: 15px;
  color: grey;
  font-size: 0.9em;
  cursor: text;
  transition: 0.3s ease;
}

.form label .input:placeholder-shown + span {
  top: 15px;
  font-size: 0.9em;
}

.form label .input:focus + span,
.form label .input:valid + span {
  top: 30px;
  font-size: 0.7em;
  font-weight: 600;
}

.form label .input:valid + span {
  color: gray;
}

.submit {
  border: none;
  outline: none;
  background-color: royalblue;
  padding: 10px;
  border-radius: 10px;
  color: #fff;
  font-size: 16px;
  transform: 0.3s ease;
}

.submit:hover {
  background-color: rgb(56, 90, 194);
}

@keyframes pulse {
  from {
    transform: scale(0.9);
    opacity: 1;
  }

  to {
    transform: scale(1.8);
    opacity: 0;
  }
}

.account-input {
  position: relative;
}

.dropdown-options {
  position: absolute;
  top: 100%;
  left: 0;
  width: 100%;
  background-color: white;
  border: 1px solid rgba(105, 105, 105, 0.397);
  border-radius: 10px;
  z-index: 10;
  margin-top: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.option {
  padding: 10px;
  cursor: pointer;
}

.option:hover {
  background-color: #f0f0f0;
}

.option:first-child {
  border-radius: 10px 10px 0 0;
}

.option:last-child {
  border-radius: 0 0 10px 10px;
}
</style>
